//
//  HomeTableViewController.swift
//  weibo
//
//  Created by 罗心 on 16/6/25.
//  Copyright © 2016年 罗心. All rights reserved.
//

import UIKit
import AFNetworking

private let reuseId = "reuseIdentifier"

class HomeTableViewController: LXVisitorViewController {

    var statusArray:[LXStatus]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if  !isLogin {
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            return
            
        }
        
        setupTableViewInfo()
        
        loadStatusData { (result) -> () in
            self.statusArray = result
        
            self.tableView.reloadData()

        }
     
    }
    
    
    
    func setupTableViewInfo(){
        tableView.registerClass(LXStatusCell.self, forCellReuseIdentifier: reuseId)
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
//        tableView.rowHeight = 300
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension HomeTableViewController{

    func loadStatusData(callBack:(result: [LXStatus])->()){
        
        LXNetworkTools.sharedTools.homeLoadStatusData({ (response) -> () in
            
            guard let res = response?["statuses"] as?[[String: AnyObject]] else {
                return
            }
            // 临时数组
            var tempArray:[LXStatus] = [LXStatus]()
            // 遍历数组
            for dic in res {
                let status = LXStatus(dict: dic)
                 tempArray.append(status)
            }

            callBack(result: tempArray)
            
        }) { (error) -> () in
            print("失败:\(error)")
        }
    }

}
    

    // MARK: - Table view data source
extension HomeTableViewController{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusArray?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseId, forIndexPath: indexPath) as! LXStatusCell
        //给cell赋值
        cell.ststus = statusArray![indexPath.row]
        return cell
    }
    
}





